package com.besall.allbase.view.activity.chipstoollevel4.protocol;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.util.Log;

import com.besall.allbase.common.utils.FileUtils;

import bb.lanxing.manager.SharedManager;

public class ProtocolReceiver extends BroadcastReceiver {

    private static final String TAG = "ProtocolReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG," the action is: " + action);
        if(Intent.ACTION_TIME_CHANGED.equals(action)){
            Log.d(TAG," send the cmd to sync time for watch ");
            ProtocolPresenter protocolPresenter = ProtocolPresenter.getInstance();
            String deviceAddress = SharedManager.getInstance().getCurConnDeviceAddress();
            Log.d(TAG, " the address is: " + deviceAddress);
            if(protocolPresenter != null && deviceAddress != null){
                Log.d(TAG, " begin send cmd to sync time ");
                FileUtils.sendCmdToSyncTime(protocolPresenter,deviceAddress);
            }

        }else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(device == null){// || !device.equals(btDevice)
                Log.d(TAG," the device is not bt device sent by user: ");
                return;
            }

       //     Log.d(TAG," "  + )
            int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);
            Log.d(TAG," the state: " + state);
            if(state == BluetoothDevice.BOND_BONDED){
                Log.d(TAG," bond success: ");
            }else if(state == BluetoothDevice.BOND_BONDING){
                Log.d(TAG," bonding: ");
            }else if(state == BluetoothDevice.BOND_NONE){
                Log.d(TAG," none bond ");
            }
/*            String  name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            Log.d(TAG," bind name: " + name);

            String  key = intent.getStringExtra(BluetoothDevice.EXTRA_PAIRING_KEY);
            Log.d(TAG," bind key: " + key);*/
        }
    }
}
